package com.doudou.oauth.validate.image;

import com.doudou.oauth.validate.AbstractValidateCodeProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 傻男人
 * @Date: 2020/5/21 15:27
 * @Version: 1.0
 * @Description: 图片验证码处理器
 */
@Slf4j
@Component("imageValidateCodeProcessor")
public class ImageCodeProcessor extends AbstractValidateCodeProcessor<ImageCode> {

	/**
     * 发送图形验证码，将其写到响应中
	 */
	@Override
	protected void send(ServletWebRequest request, ImageCode imageCode) throws Exception {
		HttpServletResponse response = request.getResponse();
		request.getResponse().setContentType("image/jpeg");
		ImageIO.write(imageCode.getImage(), "JPEG", response.getOutputStream());
	}

}